package com.itheima.util;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MqUtil {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(User user, String type) {
        //构造Map集合
        Map<String, Object> msg = new HashMap<>();
        msg.put("userId", user.getId());
        msg.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        msg.put("type", type);
        String message = JSON.toJSONString(msg);
        //发送消息
        try {
            amqpTemplate.convertSendAndReceive("tanhua.log.exchange",
                    "log.user",message);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
